<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Sys extends MY_Controller {
	
    public function __construct(){
        parent::__construct();
        $this->load->model('dbschema_model','mdl_dbschema');
    }

    public function index(){
        
        $arr_exsits_table = $this->cm->show_tables();
        
        require_once LIBDIR.'File.php';
        $obj_file = new File();
        $schema_dir = SCHDIR;
        $list_file = $obj_file->getFile($schema_dir);
        
        foreach ($list_file as $k=>$v){
            require_once SCHDIR.$v;
        }
        
        //schema 文件最后一次修改时间
        $dbschemafmtime = (array)$this->cm->get_kv('dbschemafmtime');
        
        foreach ($db as $k=>$v){
            
            //当前schema 文件的修改时间
            $dbschema_cfmtime = filemtime(SCHDIR.$k.'.php');
            $dbschema_lfmtime = $dbschemafmtime[$k];

            //表已存在且文件内容发生了变化修改表
            if(in_array($k, $arr_exsits_table) && ($dbschema_cfmtime != $dbschema_lfmtime)){
               
                $this->mdl_dbschema->modify($k,$v);
                
                $dbschemafmtime[$k] = $dbschema_cfmtime;
                
            //表不存在创建表
            }elseif(!in_array($k, $arr_exsits_table)){
             
                $sql = "CREATE TABLE IF NOT EXISTS `".$k."`(";
                
                $pk = '';
                
                foreach ($v['columns'] as $kk=>$vv){
                    $type = '';
                    if($vv['type'] == 'number' || $vv['type'] == 'time'){
                        $type = 'INT(11)';
                    }elseif($vv['type'] == 'bool'){
                        $type = 'TINYINT(1)';
                    }else{
                        $type = $vv['type'];
                    }

                    $not_null = 'NULL';
                    if(!empty($vv['required']) && $vv['required'] === true){
                        $not_null = 'NOT NULL';
                    }
                    
                    $extra = '';
                    if(!empty($vv['extra'])){
                        $extra = $vv['extra'];                        
                        $pk = "PRIMARY KEY (`".$kk."`)";
                    }
                    
                    $default = '';
                    if(!empty($vv['default'])){
                        if($vv['type'] == 'bool'){
                            $default = $vv['default'] === true ? '1' : '0';
                        }
                        $default = $vv['default'];
                    }
                    
                    $default = empty($default) ? '' : "DEFAULT '".$default."'";
                    
                    $comment = empty($vv['label']) ? '' : 'COMMENT \''.$vv['label'].'\'';
                    
                    $sql .= "`".$kk."` ".$type." ".$not_null." ".$default." ".$extra." ".$comment.",";
                            
                }
                
                $sql .= $pk;
                
                $sql .= ") ";
                
                $engine = empty($v['engine']) ? 'ENGINE=MyISAM' : 'ENGINE='.$v['engine'];
                $tbl_comment = empty($v['comment']) ? '' : "COMMENT='".$v['comment']."'";
                        
                $sql .= $engine." DEFAULT CHARSET=utf8 ".$tbl_comment." AUTO_INCREMENT=1;";
                
                $flag = $this->cm->query($sql);
                
                //保存创建记录到 kvstore 中
                if($flag){
                    $dbschemafmtime[$k] = filemtime(SCHDIR.$k.'.php');
                }
                
            }       
                    
        }
        
        $this->cm->set_kv('dbschemafmtime',$dbschemafmtime);
        
	}
	
	
    public function add(){
        
	}
}

